<?php
class ModelVoucherOrdertemp extends Model 
{
	public function getList()
	{
		$query = $this->db->query("Select * from `order_temp`");
		return $query->rows;
	}
	
	public function getItem($orderid)
	{
		$query = $this->db->query("Select * from `order_temp` where orderid = '".$orderid."'");
		return $query->row;
	}
	
	public function nextID($prefix)
	{
		return $this->db->getNextIdVarChar("order_temp","ordernumber",$prefix);
	}
	
	public function insertdefault($ordertype)
	{
		$prefix = $ordertype.date("Y").date("m")."_";
		$data['ordernumber'] = $this->nextID($prefix);
		$data['ordertype'] = $ordertype;
		$data['orderdate'] = $this->date->getToday();
		$data['paymenttype'] = "cash";
		  
		return $this->insert($data);
	}
	
	public function saveorder($data)
	{
		$orderid=$this->db->escape(@$data['orderid']);
		$ordernumber=$this->db->escape(@$data['ordernumber']);
		$ordertype=$this->db->escape(@$data['ordertype']);
		$orderdate=$this->db->escape(@$data['orderdate']);
		$partnerid=$this->db->escape(@$data['partnerid']);
		$employeeid=$this->db->escape(@$data['employeeid']);
		$paymenttype=$this->db->escape(@$data['paymenttype']);
		$subtotal=(float)(@$data['subtotal']);
		$taxamount=(float)(@$data['taxamount']);
		$discountamount=(float)(@$data['discountamount']);
		$grandtotal=(float)(@$data['grandtotal']);
		$paymentamount=(float)(@$data['paymentamount']);
		$deptamount=(float)(@$data['deptamount']);
		
		$field=array(
						'ordernumber',
						'ordertype',
						'orderdate',
						'partnerid',
						'employeeid',
						'paymenttype',
						'subtotal',
						'taxamount',
						'discountamount',
						'grandtotal',
						'paymentamount',
						'deptamount'
					);
		$value=array(
						$ordernumber,
						$ordertype,
						$orderdate,
						$partnerid,
						$employeeid,
						$paymenttype,
						$subtotal,
						$taxamount,
						$discountamount,
						$grandtotal,
						$paymentamount,
						$deptamount
					);
		
		$order = $this->getItem($orderid);
		if(count($order) > 0)
		{
			$where="orderid = '".$orderid."'";
			$this->db->updateData("order_temp",$field,$value,$where);
			return $orderid;
		}
		else
		{
			return $this->db->insertData("order_temp",$field,$value);	
		}
	}
	
	public function insert($data)
	{
		$orderid=$this->db->escape(@$data['orderid']);
		$ordernumber=$this->db->escape(@$data['ordernumber']);
		/*if($ordernumber == "")
		{
			$ordernumber = $this->nextID($this->db->escape(@$data['ordernumber_prefix']));
		}
*/		
		$ordertype=$this->db->escape(@$data['ordertype']);
		$orderdate=$this->db->escape(@$data['orderdate']);
		$partnerid=$this->db->escape(@$data['partnerid']);
		$employeeid=$this->db->escape(@$data['employeeid']);
		$paymenttype=$this->db->escape(@$data['paymenttype']);
		$subtotal=(float)(@$data['subtotal']);
		$taxamount=(float)(@$data['taxamount']);
		$discountamount=(float)(@$data['discountamount']);
		$grandtotal=(float)(@$data['grandtotal']);
		$paymentamount=(float)(@$data['paymentamount']);
		$deptamount=(float)(@$data['deptamount']);
		
		$field=array(
						'ordernumber',
						'ordertype',
						'orderdate',
						'partnerid',
						'employeeid',
						'paymenttype',
						'subtotal',
						'taxamount',
						'discountamount',
						'grandtotal',
						'paymentamount',
						'deptamount'
					);
		$value=array(
						$ordernumber,
						$ordertype,
						$orderdate,
						$partnerid,
						$employeeid,
						$paymenttype,
						$subtotal,
						$taxamount,
						$discountamount,
						$grandtotal,
						$paymentamount,
						$deptamount
					);
		return $this->db->insertData("order_temp",$field,$value);
	}
	
	public function update($data)
	{
		$orderid=$this->db->escape(@$data['orderid']);
		$ordernumber=$this->db->escape(@$data['ordernumber']);
		if($ordernumber == "")
		{
			$ordernumber = $this->nextID($this->db->escape(@$data['ordernumber_prefix']));
		}
		
		$ordertype=$this->db->escape(@$data['ordertype']);
		$orderdate=$this->db->escape(@$data['orderdate']);
		$partnerid=$this->db->escape(@$data['partnerid']);
		$employeeid=$this->db->escape(@$data['employeeid']);
		$paymenttype=$this->db->escape(@$data['paymenttype']);
		$subtotal=(float)(@$data['subtotal']);
		$taxamount=(float)(@$data['taxamount']);
		$discountamount=(float)(@$data['discountamount']);
		$grandtotal=(float)(@$data['grandtotal']);
		$paymentamount=(float)(@$data['paymentamount']);
		$deptamount=(float)(@$data['deptamount']);
		
		$field=array(
						'ordernumber',
						'ordertype',
						'orderdate',
						'partnerid',
						'employeeid',
						'paymenttype',
						'subtotal',
						'taxamount',
						'discountamount',
						'grandtotal',
						'paymentamount',
						'deptamount'
					);
		$value=array(
						$ordernumber,
						$ordertype,
						$orderdate,
						$partnerid,
						$employeeid,
						$paymenttype,
						$subtotal,
						$taxamount,
						$discountamount,
						$grandtotal,
						$paymentamount,
						$deptamount
					);
					
		$where="orderid = '".$orderid."'";
		$this->db->updateData("order_temp",$field,$value,$where);
	}	
			
	public function delete($orderid)
	{
		$orderid=$this->db->escape(@$orderid);
		$where="orderid = '".$orderid."'";
		$this->db->deleteData('order_temp',$where);
	}
	
	public function deletes($data)
	{
		foreach($data as $orderid)
		{
			$this->delete($orderid);
		}		
	}
	
}

?>